< previous page page_339 next page >

Page 339
0339-01.jpg
Figure 11.11
Program LeastSq3 fitting points with a degree 4 polynomial.
Subroutine GetLeastSquareValues creates the coefficient arrays representing the partial derivatives. It calls subroutine GaussianEliminate to perform the Gaussian elimination. It copies the polynomial's coefficients into the a_values array for use by the PolynomialValue function.
e10a6a0a59f035e870d90cca05b85fce.gif
' Draw the least squares line.
Private Sub DrawCurve(ByVal degree As Integer)
Dim a_values() As Single
Dim x1 As Single
Dim x2 As Single
Dim i As Integer
Dim X As Single
Dim dx As Single

    ' Get the parameters for the quadratic.
    If GetLeastSquaresValues(degree, PtX, PtY, a_values) Then
        ' There is a solution.
        ' Find the minimum and maximum X values.
        x1 = PtX(1) ' This will be the minimum X value.
        x2 = x1     ' This will be the maximum X value.
        For i = 2 To NumPts
            If x1 > PtX(i) Then x1 = PtX(i)
            If x2 < PtX(i) Then x2 = PtX(i)
        Next i

        ' Draw the curve.
        picCanvas.CurrentX = x1
        picCanvas.CurrentY = PolynomialValue(a_values, x1)

 
< previous page page_339 next page >